import pgzrun


WIDTH = 400
HEIGHT = 400



board = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None]

def GetActorList():
    NUM_PICTURE_LIST = []
    num = 0
    for y in range(0, 304, 101):
        for x in range(0, 304, 101):
            num += 1
            if num == 16:
                break
            NUM_PICTURE_LIST.append(
                Actor(
                    f'{num}.png' if len(str(num)) == 2 else f'0{num}.png',
                    [x + 50, y + 50]
                )
            )
    return NUM_PICTURE_LIST

def DexToArray(dex):
    x = dex // 4
    y = dex % 4
    return (x, y)

def ArrayToDex(x, y):
    dex = y * 4 + x
    if dex < 0:
        dex = 0
    return dex

def GetNum(dex):
    global board
    return board[dex]

def JudgePos(x, y):
    sqX = x // 51
    sqY = y // 51
    return (sqX, sqY)

class Click:
    def __init__(self, pos):
        self.pos = pos
        self.sqPos = (pos[0] // 101, pos[1] // 101)
        self.sqX = pos[0]
        self.sqY = pos[1]
        self.dex = ArrayToDex(self.sqPos[0], self.sqPos[1])
        self.num = GetNum(self.dex)
    
    def OutPut(self):
        print(f'pos = {self.pos}')
        print(f'sqPos = {self.sqPos}')
        print(f'dex = {self.dex}')
        print(f'num = {self.num}')
    
    def CanSwap(self):
        global board
        try:
            a = DexToArray(board.index('-1'))
        except ValueError:
            a = DexToArray(board.index(None))
        if abs(self.sqX - a[0]) == 1 or abs(self.sqY - a[1]) == 1:
            if self.sqX - a[0] == 1:
                return 'left'
            elif a[0] - self.sqX == 1:
                return 'right'
            elif self.sqY - a[1] == 1:
                return 'up'
            elif a[1] - self.sqY == 1:
                return 'down'
        else:
            return False
    
    def Swap(self, objDex):
        global board
        board[self.dex], board[objDex] = board[objDex], board[self.dex]
    
    def GetClickedNum(self):
        return self.num

def SwapBoard(dex, wh):
    global NUM_PICTURE_LIST
    screen.fill('#000000')
    if wh == 'up':
        for i in range(101):
            screen.fill('#000000')
            NUM_PICTURE_LIST[dex].y -= 0
    elif wh == 'down':
        for i in range(101):
            screen.fill('#000000')
            NUM_PICTURE_LIST[dex].y += 0

def on_mouse_down(pos):
    global board
    Click(pos).OutPut()
    ID = Click(pos)
    if ID.CanSwap() != False:
        SwapBoard(ID.dex, ID.CanSwap())
        try:
            ID.Swap(board[board.index(-1)])
        except:
            ID.Swap(board[board.index(None)])
    # Mr. Xue, there seems to be something wrong here. On my
    # computer, clicking the box after running will only output.


NUM_PICTURE_LIST = GetActorList()

def draw():
    for i in NUM_PICTURE_LIST:
        i.draw()

def update():
    screen.fill('#000000')
    # Here, the two pieces to be exchanged are exchanged
    # directly. The interval between each two pieces is 51.

pgzrun.go()
